package com.vsoft.sms_cm;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.util.LogCollect;

public class RedisCheck {
	private Jedis vJedis  = null;
	private static Logger vLogger = Logger.getLogger(RedisCheck.class);
	public static void main(String[] args) throws BaseException {
		Configuration.InitSystem();
		RedisCheck t = new RedisCheck();
		t.init();
		t.check();
	
	}
	private void init() throws BaseException{
		BusiObj confBo = Configuration.getConfBO();
		BusiObj bo = confBo.getBusiObj("REDIS_SMS");
		vJedis = new Jedis(bo.getString("ServerAddress"),bo.getInt("Port","6379"),15000);
//		vJedis = new Jedis("172.18.1.10",6379);
	}
	private void check() throws BaseException {
		
			Statement st = null;
			ResultSet rs = null;
			StringBuffer strBuffer = new StringBuffer();
			String strInput = null;
			byte[] byteData = null;
			int iRead = 0, iLen = 0;
			boolean bStart = true, bExec = false;
			System.out.println("Please input the key value，如想退出，键入exit或者quit");
			try{
			while (true) {
				if (bStart) {
					System.out.print("Data > ");
				}
				try {
					byteData = new byte[10];
					iRead = System.in.read(byteData);
					if (iRead > 1) {
						if (byteData[iRead - 2] == 13) {
							byteData[iRead - 2] = 0;
							byteData[iRead - 1] = 0;
							iRead = iRead - 2;
						} else if (byteData[iRead - 1] == 13) {
							byteData[iRead - 1] = 0;
							iRead--;
						}
					} else if (iRead == 1) {
						byteData[0] = 0;
						iRead--;
					}
					bStart = false;
					if (iRead > 0)
						strInput = new String(byteData, 0, iRead);
					else
						strInput = "";
					byteData = null;
					strInput.trim();
					strBuffer.append(strInput);
					if (System.in.available() == 0) // 读取完毕
					{
						strInput = strBuffer.toString();
						strInput = strInput.trim();
						bStart = true;
						if (strInput.equals("") == false) {
							if (strInput.length() < 10)
								if (strInput.toUpperCase().equals("EXIT")
										|| strInput.toUpperCase().equals("QUIT")) {
									break;
								}
							iLen = strInput.length();
							if (strInput.substring(iLen - 1, iLen).equals(";")
									|| strInput.substring(iLen - 1, iLen).equals(
											"/")) {
								// 执行SQL语句
								strInput = strInput.substring(0,
										strInput.length() - 1);
								strBuffer.setLength(0);
								bExec = false;
								
								//
								String strVal = vJedis.get(strInput);
								Object obj = null;
								ByteArrayInputStream bais = null;
								ObjectInputStream ios = null;
								if(strVal != null){
									try{
										 bais = new ByteArrayInputStream(strVal.getBytes("iso-8859-1"));//SafeEncoder.encode(strVal));
										 ios = new ObjectInputStream(bais);
										obj = ios.readObject();
										if(obj == null){
											System.out.print("convert object failure :"+strVal);
										}
											
										if(obj instanceof ArrayList){
											ArrayList<BusiObj> arData = (ArrayList<BusiObj>)obj;
											System.out.print("total :"+arData.size());
											for(BusiObj m :arData){
												System.out.println(m.getRawData().toString());
												System.out.println("----------------------");
											}
										}
									}catch(Exception e){
											throw new BaseException("0002","从缓存获取数据出错:"+LogCollect.getStackLog(e));
									}finally{
										if(bais != null)
											bais.close();
										if(ios != null)
											ios.close();
									}
								}else{
									System.out.print("no data");
								}
							
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			}finally{
				
				
			byteData = null;
			strBuffer = null;
			strInput = null;
			}
		
	}
}
